package com.tuyan.aixiang.component;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.common.lang.VoidFunction;

import androidx.annotation.NonNull;

public class CommonDialog extends Dialog {

    private TextView tvTitle;

    private TextView tvContent;

    private TextView tvConfirm;

    private TextView tvCancel;

    private VoidFunction onConfirm;

    private VoidFunction onCancel;

    public CommonDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_dialog);
        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
        tvConfirm = findViewById(R.id.tv_positive);
        tvCancel = findViewById(R.id.tv_negative);
        findViewById(R.id.rl_positive).setOnClickListener(v -> {
            if(null != onConfirm) onConfirm.call();
        });
        findViewById(R.id.rl_negative).setOnClickListener(v -> {
            if(null != onCancel) onCancel.call();
            dismiss();
        });
    }

    public CommonDialog setTitle(String title) {
        tvTitle.setText(title);
        return this;
    }

    public CommonDialog setContent(String content) {
        tvContent.setText(content);
        return this;
    }

    public CommonDialog setPositiveText(String text) {
        tvConfirm.setText(text);
        return this;
    }

    public CommonDialog setNegativeText(String text) {
        tvCancel.setText(text);
        return this;
    }

    public CommonDialog setOnPositive(VoidFunction func) {
        onConfirm = func;
        return this;
    }

    public CommonDialog setOnNegative(VoidFunction func) {
        onCancel = func;
        return this;
    }
}
